#include <cstdlib>

#include <iostream>

#include <LLIIO/LLIIO.h>

#include <LLIIO/EXRImage.h>
#include <LLIIO/PNGImage.h>

LtImage LiOpen( const char * szPath )
{
    LtImage pRet = NULL ;

    Image * pImage = NULL ;
    if ( strstr( szPath , "exr" ) )
    {
        pImage = new EXRImage ;
    }
    else if ( strstr( szPath , "png" ) )
    {
        pImage = new PNGImage ;
    }
    if ( pImage )
    {
        if ( pImage->Open( szPath ) )
        {
            pRet = reinterpret_cast< LtImage >( pImage ) ;
        }
    }

    return pRet ;
}

LtInt32 LiGetWidth( const LtImage pI )
{
    LtInt32 iRet = 0 ;

    if ( pI )
    {
        Image * pImage = reinterpret_cast< Image * >(pI) ;
        iRet = pImage->GetWidth() ;
    }

    return iRet ;
}

LtInt32 LiGetHeight( const LtImage pI )
{
    LtInt32 iRet = 0 ;

    if ( pI )
    {
        Image * pImage = reinterpret_cast< Image * >(pI) ;
        iRet = pImage->GetHeight() ;
    }

    return iRet ;
}

LtImage LiCreate( LtInt32 iBPP , LtInt32 iW , LtInt32 iH , const LtInt8U * pData )
{
    LtImage pRet = NULL ;

    int iSize = iW * iH ;
    switch( iBPP )
    {
    case LLIIO_RGB8 :
        iSize *= 3 * sizeof( char ) ;
        break ;
    case LLIIO_RGBA8 :
        iSize *= 4 * sizeof( char ) ;
        break ;
    case LLIIO_RGB16 :
        iSize *= 3 * sizeof( short ) ;
        break ;
    case LLIIO_RGBA16 :
        iSize *= 4 * sizeof( short ) ;
        break ;
    case LLIIO_RGB32 :
        iSize *= 3 * sizeof( int ) ;
        break ;
    case LLIIO_RGBA32 :
        iSize *= 4 * sizeof( int ) ;
        break ;
    case LLIIO_RGB32F :
        iSize *= 3 * sizeof( float ) ;
        break ;
    case LLIIO_RGBA32F :
        iSize *= 4 * sizeof( float ) ;
        break ;
    default :
        iSize = 0 ;
    }
    if ( iSize )
    {
        Image * pImage = new Image ;
        if ( pImage->Allocate( iSize ) )
        {
            memcpy( pImage->m_pData.get() , pData , iSize ) ;
            pRet = reinterpret_cast<LtImage>(pImage) ;
        }
    }

    return pRet ;
}

LtVoid LiClose( LtImage * ppI )
{
    Image * pImage = reinterpret_cast<Image *>( * ppI ) ;
    if ( pImage )
    {
        pImage->Close() ;
        delete pImage ;

        * ppI = NULL ;
    }
}

/*
int main( int iArgc , char * aArgv )
{


    //
    FILE * pFile = fopen("C:/Head.png", "rb") ;

    png_byte aPNGHeader[8];
    fread(aPNGHeader, 1, 8, pFile);
    std::cout << png_sig_cmp(aPNGHeader, 0, 8) << std::endl;

    png_structp pPNGReadStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    
    png_infop pPNGInfoStruct = png_create_info_struct(pPNGReadStruct);

    png_init_io(pPNGReadStruct, pFile);
    png_set_sig_bytes(pPNGReadStruct, ftell(pFile));

    png_read_info(pPNGReadStruct, pPNGInfoStruct);

    png_uint_32 uW , uH ;
    int iBD , iCT ;
    png_get_IHDR(pPNGReadStruct, pPNGInfoStruct, & uW, & uH, & iBD, &iCT,
        NULL, NULL, NULL);

    std::cout << png_get_rowbytes(pPNGReadStruct, pPNGInfoStruct);

    return EXIT_SUCCESS ;
}
*/
